﻿
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace CmdPrep.Frontends
{
    public partial class MenuForm : Form
    {
        #region Private data

        Timer cmdListBoxLongMousePressTimer_  = new Timer();

        #endregion

        #region Public events

        public delegate void CmdFileListRequestEventHandler(string projectName);
        public event CmdFileListRequestEventHandler cmdFileListRequested;

        public delegate void SdlFileListRequestEventHandler(string projectName);
        public event SdlFileListRequestEventHandler sdlFileListRequested;
                        
        public delegate void InputCmdFileRequestEventHandler(string hintDirName);
        public event InputCmdFileRequestEventHandler inputCmdFileRequested;

        public delegate void OutputCmdFileRequestEventHandler(string hintFileName);
        public event OutputCmdFileRequestEventHandler outputCmdFileRequested;
        
        public delegate void ProcessCmdFileRequestEventHandler(string inputFullName, 
                                                               string outputFullName,
                                                               bool keepEqualSign,
                                                               bool skipEmptyLines);
        public event ProcessCmdFileRequestEventHandler processCmdFileRequested;
        
        public delegate void DeleteCmdFileRequestEventHandler(string fullName);
        public event DeleteCmdFileRequestEventHandler deleteCmdFileRequested;
        
        public delegate void EditCmdFileRequestEventHandler(string fullName);
        public event EditCmdFileRequestEventHandler editCmdFileRequested;
                        
        public delegate void DiffCmdFilesRequestEventHandler(string fullName1, string fullName2);
        public event DiffCmdFilesRequestEventHandler diffCmdFilesRequested;
                        
        public delegate void EditProgramSettingsRequestEventHandler();
        public event EditProgramSettingsRequestEventHandler editProgramSettingsRequested;
        
        public delegate void StatusChangedEventHandler(string statusText);
        public event StatusChangedEventHandler statusChanged;

        public delegate void ExitEventHandler();
        public event ExitEventHandler exitRequested;

        #endregion

        #region Public foundation

        public MenuForm() 
        {
            InitializeComponent();
            
            // Connect events
            statusChanged                           += (statusText) => toolStripStatusLabel.Text = statusText;
            
            cmdUserControl.statusChanged            += (statusText) => statusChanged(statusText);
            cmdUserControl.inputCmdFileRequested    += () => inputCmdFileRequested(projectsComboBox.SelectedValue as string);
            cmdUserControl.outputCmdFileRequested   += (fileNameHint) => outputCmdFileRequested(fileNameHint);
            cmdUserControl.processCmdFileRequested  += (inputFullName, outputFullName, keepEqualSign, skipEmptyLines) => processCmdFileRequested(inputFullName, outputFullName, keepEqualSign, skipEmptyLines);
            cmdUserControl.deleteCmdFileRequested   += (fullName) => deleteCmdFileRequested(fullName);
            cmdUserControl.editCmdFileRequested     += (fullName) => editCmdFileRequested(fullName);
            cmdUserControl.diffCmdFilesRequested    += (fullName1, fullName2) => diffCmdFilesRequested(fullName1, fullName2);
            
            sdlUserControl.statusChanged            += (statusText) => statusChanged(statusText);
        }

        #endregion

        #region Public methods

        public void setProjectsDataTable(DataTable dataTable)
        {
            if (dataTable == null)
                return;
            
            projectsComboBox.DataSource     = dataTable;
            projectsComboBox.ValueMember    = dataTable.Columns["QDIR"].ColumnName;
            projectsComboBox.DisplayMember  = dataTable.Columns["PNAME"].ColumnName;
        }
        
        public void setProjectFilesDataTable(DataTable dataTable) 
        {
            reportFilesListBox.DataSource    = dataTable;
            reportFilesListBox.ValueMember   = dataTable.Columns["fullName"].ColumnName;
            reportFilesListBox.DisplayMember = dataTable.Columns["fileName"].ColumnName;
        }
        
        public void addInputCmdFileIfExclusive(string inputCmdFullName)
        {
            cmdUserControl.addInputCmdFileIfExclusive(inputCmdFullName);
        }
        
        public void addOutputCmdFile(string outputCmdFullName)
        {
            cmdUserControl.addOutputCmdFile(outputCmdFullName);
        }
        
        #endregion
        
        #region Private event handlers

        private void handleProjectsComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox projectsComboBox = sender as ComboBox;
            
               DataTable projectsDataTable = projectsComboBox.DataSource as DataTable;
            
            string columnName = 
                (tabControl.SelectedTab == CMDtab) ? "QDIR" :
                (tabControl.SelectedTab == SDLtab) ? "ZDIR" :
                /* otherwise */                      "";
 
            string reportFileFullName = (projectsDataTable == null)
                ? null
                : projectsDataTable.Rows[projectsComboBox.SelectedIndex][columnName] as string ;
                        
            if (tabControl.SelectedTab == CMDtab)
                cmdFileListRequested.Invoke(reportFileFullName);
            
            if (tabControl.SelectedTab == SDLtab)
                sdlFileListRequested.Invoke(reportFileFullName);
        }
        
        private void handleCmdListBoxDoubleClick(object sender, System.EventArgs e)
        {
            ListBox reportFilesListBox = sender as ListBox;

            int index = (reportFilesListBox == null)
                ? ListBox.NoMatches
                : reportFilesListBox.IndexFromPoint(reportFilesListBox.PointToClient(Cursor.Position));
           
            string reportFileFullName = (index == ListBox.NoMatches)
                ? null
                : reportFilesListBox.SelectedValue as string;
           
            if (! string.IsNullOrEmpty(reportFileFullName))
            {
                        if (tabControl.SelectedTab == CMDtab)
                        cmdUserControl.addInputCmdFileIfExclusive(new FileInfo(reportFileFullName));
                       
                        if (tabControl.SelectedTab == SDLtab)
                        sdlUserControl.addInputSdlFileIfExclusive(new FileInfo(reportFileFullName));

                statusChanged("CMD file added: " +reportFileFullName);
            }
        }
        
        private void handleListBox2SelectedValueChanged(object sender, System.EventArgs e)
        {
            if (statusChanged != null)
                statusChanged(debugListBox.SelectedItem.ToString());
        }
        
        private void handleExitButtonClick(object sender, EventArgs e)
        {
            if (exitRequested != null)
                exitRequested();
        }
        
        private void handleProjectContentListBoxMouseHover(object sender, EventArgs e)
        {            
            ListBox listBox = sender as ListBox;
            if (listBox == null)
                return;
            
            int index = listBox.IndexFromPoint(listBox.PointToClient(Cursor.Position));
            string fullName = ((index >= 0) && (index < listBox.Items.Count))
                ? (listBox.Items[index] as DataRowView).Row["fullName"] as string
                : null;
            
            if (string.IsNullOrEmpty(fullName))
                projectContentToolTip.RemoveAll();
            else
                projectContentToolTip.SetToolTip(listBox, fullName);            
        }
        
        private void handlePropertiesToolStripMenuItemClicked(object sender, EventArgs e)
        {
            editProgramSettingsRequested();
        }
        
        private void handleTabControlSelected(object sender, TabControlEventArgs e)
        {
            TabControl tabControl = sender as TabControl;
            
            DataTable projectsDataTable = projectsComboBox.DataSource as DataTable;
            
            string columnName = 
                (tabControl.SelectedTab == CMDtab) ? "QDIR" :
                (tabControl.SelectedTab == SDLtab) ? "ZDIR" :
                (tabControl.SelectedTab == CMDgenTab) ? "QDIR" :
                /* otherwise */                      "";            
            
            string reportFileFullName = (projectsDataTable == null)
                ? null
                : projectsDataTable.Rows[projectsComboBox.SelectedIndex][columnName] as string ;            
            
            if (tabControl.SelectedTab == CMDtab)
                cmdFileListRequested.Invoke(projectsComboBox.SelectedValue as string);
            
            if (tabControl.SelectedTab == SDLtab)
                sdlFileListRequested.Invoke(reportFileFullName);
            
            if (tabControl.SelectedTab == CMDgenTab)
                ;//MessageBox.Show("CMD generation comes here",null, MessageBoxButtons.OK) ;
        }
        
        #endregion
    }
}
